from drf_spectacular.utils import extend_schema, inline_serializer
from rest_framework import status as ResponseStatus
from rest_framework.decorators import action
from rest_framework.mixins import (CreateModelMixin, ListModelMixin,
                                   RetrieveModelMixin, UpdateModelMixin,
                                   )
from rest_framework.viewsets import GenericViewSet
from backend.cews.services.TaskService import filter_task_by_mtce_type, soft_delete_part
from utils.autoschema import AutoDocSchema, custom_response_serailizer
from utils.decorators import request_data, error_response, check_group_permission

import backend.cews.services.RepairService as RepairService
from backend.cews.models import RepairAction, RepairOrder, Task

from .serializers import (RepairOrderTaskSerializer, TaskSerializer)
from rest_framework.response import Response
from rest_framework import serializers

from utils.mixins import TaskCreateModelMixin , TaskUpdateModelMixin

class TaskViewSet(
    RetrieveModelMixin,
    ListModelMixin,
    TaskUpdateModelMixin,
    GenericViewSet,
    TaskCreateModelMixin,
):
    serializer_class = TaskSerializer
    queryset = Task.objects.filter(deleted=False)

    # @extend_schema(
    #     responses={
    #         200: code_data_serializer(RepairOrderTaskSerializer(many=True))
    #     }
    # )
    # @action(detail=False, methods=['get'], schema=AutoDocSchema())
    # @request_data(keys=['repair_order_id'])
    # def get_repair_order_tasks(self, request, repair_order_id):

    #     rots = query_repair_order_task_of_order(repair_order_id)
    #     rots = RepairOrderTaskSerializer(rots, many=True).data

    #     return Response(status=200,
    #                     data={
    #                         'code': 1,
    #                         'data': rots
    #                     })

    @action(detail=False, methods=['post'], schema=AutoDocSchema())
    @request_data(keys=['mtce_type'])
    def get_task_by_type(self, request, mtce_type):
        
        tasks = filter_task_by_mtce_type(mtce_type)
        
        data = TaskSerializer(tasks, many=True).data
        return Response(status=200,
                        data={
                            'code': 1,
                            'data': data
                        })
    
    @action(detail=False, methods=['delete'], schema=AutoDocSchema())
    @request_data(keys=['id'])
    @check_group_permission('delete_task')
    @error_response()
    def soft_delete_MasterTask(self, request, id):
        
        result = soft_delete_part(
            id,
            request.user
        )
        
        if result:
            response = {
                "code": 1,
                "message": "Success delete task"
            }

        return Response(status=200, data=response)
